package com.cn.bukeManage.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;
import com.cn.bukeManage.dao.APPDao;
import com.cn.bukeManage.pojo.Advertisement;
import com.cn.bukeManage.pojo.App;
import com.cn.bukeManage.pojo.Picture;
import com.cn.bukeManage.pojo.Problem;
import com.cn.bukeManage.pojo.page.AdvertisementPage;
import com.cn.bukeManage.pojo.page.AppPage;
import com.cn.bukeManage.service.APPService;
import com.cn.bukeManage.utils.upload.DeleteLocalFileUtils;
import com.cn.bukeManage.utils.upload.UploadUtils;

@Service("appService")
public class APPServiceImpl implements APPService {
	@Resource
	private APPDao appDao;

	// 通过appID获取他的所有广告
	public List<Map<String, Object>> getAllAdByPage(Model model, Integer currentPage) {
		AdvertisementPage page = new AdvertisementPage();
		page.setCurrentPage(currentPage);
		List<Map<String, Object>> advertisementPages = appDao.getAllAdByPage(page);
		for (Map<String, Object> map : advertisementPages) {
			List<Picture> list = appDao.getPictureByAdId(Integer.parseInt(map.get("adverId").toString()));
			map.put("img", list.get(0).getImg());
		}
		Integer rows = appDao.getAllRowsByAppId(page);
		page.setRows(rows);
		model.addAttribute("page", page);
		return advertisementPages;
	}

	@Override
	public List<Map<String, Object>> getAllAppBasic(Model model, Integer currentPage) {
		AppPage page = new AppPage();
		page.setCurrentPage(currentPage);
		List<App> apps = appDao.getAllAppDetail(page);
		Integer rows = appDao.getAllRowsAppDetail();
		page.setRows(rows);
		model.addAttribute("page", page);
		// 得到意见的条数
		List<Map<String, Object>> list = new ArrayList<>();
		for (App app : apps) {
			Integer problemRows = appDao.getProblemRowsByAppId(app.getAppId());
			Map<String, Object> map = new HashMap<>();
			map.put("appId", app.getAppId());
			map.put("aboutMe", app.getAboutMe());
			map.put("appName", app.getAppName());
			map.put("versionIntroduce", app.getVersionIntroduce());
			map.put("problemNum", problemRows);
			list.add(map);
		}
		return list;
	}

	public App getAppByAppId(Integer appId) {
		App app = appDao.getAppByAppId(appId);
		return app;
	}

	// 根据appId得到它的所有详细信息
	public List<Map<String, Object>> getProblemByAppId(Integer appId) {
		List<Problem> problems = appDao.getProblemByAppId(appId);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
		List<Map<String, Object>> list = new ArrayList<>();
		for (Problem problem : problems) {
			Map<String, Object> map = new HashMap<>();
			map.put("appId", problem.getAppId());
			map.put("problemCount", problem.getProblemCount());
			map.put("problemTime", simpleDateFormat.format(problem.getProblemTime()));
			list.add(map);
		}
		return list;
	}

	// app信息修修改
	public Integer updateAppData(App app) {
		Integer integer = appDao.updateAppData(app);
		return integer;
	}

	// 新增app
	public Integer addNewApp(App app) {
		Integer integer = appDao.addNewApp(app);
		return integer;
	}

	// 根据广告id获取他的所有广告图片
	public List<Picture> getPictureByAdverId(Integer adverId) {
		List<Picture> pictureByAdId = appDao.getPictureByAdId(adverId);
		return pictureByAdId;
	}

	// 根据广告id修改他的状态
	public Integer updateAdverStatus(Advertisement advertisement) {
		Integer integer = appDao.updateAdverStatus(advertisement);
		return integer;
	}

	// 上传单个广告图片
	public Integer uploadPictureOfAdver(MultipartFile pictureFile,Integer adverId,HttpServletRequest request) {
		String path = "temp/img";
		// 这个是在本地服务器上面跑
		path = request.getSession().getServletContext().getRealPath(path);
		// 文件的名字
		String name = String.valueOf(System.currentTimeMillis()) + "_" + Math.round(Math.random() * 90000 + 10000) + ""
				+ pictureFile.getOriginalFilename();
		// 上传到服务器本地
		UploadUtils.uploadLocalTomcat(path, name, pictureFile);
		// 上传到七牛
		int codeStatus = UploadUtils.uploadQiNiu(path, name);
		if (codeStatus == 200) {
			// 上传到七牛成功!
			// 图片地址存到本地数据库中
			Map<String,Object> map=new HashMap<>();
			map.put("adverId", adverId);
			map.put("img", name);
			appDao.updateAdverPictureByAdverId(map);

		} else {
			// 上传到七牛失败!
			
		}

		// 删除本地服务器里面的
		DeleteLocalFileUtils.delAllFile(path);

		return codeStatus;
	}

}
